package com.dongdongshop.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;
import java.util.Set;

@Service
@org.springframework.stereotype.Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public TbUser selectByName(String username) {
        TbUser tbUser = tbUserMapper.selectByName(username);
        return tbUser;
    }

    @Override
    public Result insert(TbUser tbUser, String code) {
        //key 中的value
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        String o = (String) redisTemplate.opsForValue().get(tbUser.getPhone());
        if (code.equals(o)){
            try {
                tbUserMapper.insert(tbUser);
                return Result.build(ConstantEnum.SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.ERROR);
    }
}
